Algorithm Analysis: Time Complexity

PolyU DSAI2201 Lecture 13 2025-11-25

Defining Time Complexity: The Big OO Notation

Algorithm Analysis quantifies the resource requirements (time and space) of an algorithm relative to the size of the input (NN).

We use Big OO Notation (O(f(N))O(f(N))) to describe the worst-case rate of growth (upper bound) of an algorithm's running time as NN \to \infty.

Key Principles of Big OO Analysis:
  • Focus on Dominant Terms: Drop lower-order terms (e.g., N2+NN^2 + N becomes O(N2)O(N^2)).
  • Ignore Constants: Drop multiplicative constants (e.g., 5N25N^2 becomes O(N2)O(N^2)).
  • Worst Case: We generally calculate the performance based on the scenario that maximizes operations.

Big OO Calculation Cheat Sheet

  • O(1)O(1) Check: If the code execution time does not depend on the input size NN (e.g., arithmetic, assignment, array lookup), it is O(1)O(1).
  • Loop Rule (Multiplication): If Loop A (O(A)O(A)) contains Loop B (O(B)O(B)), the complexity is O(A×B)O(A \times B). If BB is a constant, the complexity remains O(A)O(A).
  • Sequential Rule (Addition): If operation A (O(A)O(A)) is followed by operation B (O(B)O(B)), the complexity is O(max(A,B))O(\text{max}(A, B)).
  • Logarithmic Hint: Any algorithm that repeatedly halves the search space (e.g., dividing NN by 2 each step) is O(logN)O(\log N).

Standard Time Complexity Classes

Understanding these fundamental growth rates is crucial for efficient system design. Note the difference in scale!

NotationNameGrowth Rate (N=1 Million)Example Operation
O(1)O(1)ConstantInstantaneousArray Index Access (arr[i])
O(logN)O(\log N)LogarithmicVery Fast (20 Ops)Binary Search
O(N)O(N)Linear1 Million OpsSingle Loop Iteration
O(NlogN)O(N \log N)Log-Linear~20 Million OpsEfficient Sorting (Merge Sort)
O(N2)O(N^2)Quadratic1 Trillion OpsNested Loops (Brute Force)
📝 Interactive Quiz

1. Analyze the following Python snippet. Assume `N` is the length of the list `data`.

def example_function(data):
    N = len(data)
    total = 0
    # Outer Loop: Runs N times
    for i in range(N):
        # Inner Loop: Runs a constant 5 times
        for j in range(5):
            total += data[i]

What is the Big OO time complexity of `example_function`?

  • A) O(N2)O(N^2)
  • B) O(N)O(N)
  • C) O(5N)O(5N)
  • D) O(1)O(1)

2. An algorithm has two sequential parts: one is O(N)O(N) and the other is O(N2)O(N^2). What is the overall time complexity?

  • A) O(N)O(N)
  • B) O(N2)O(N^2)
  • C) O(N+N2)O(N + N^2)
  • D) O(N3)O(N^3)

3. Which of the following complexity classes represents the fastest growth rate (i.e., is the least efficient for large inputs)?

  • A) O(logN)O(\log N)
  • B) O(N)O(N)
  • C) O(NlogN)O(N \log N)
  • D) O(N2)O(N^2)

4. Analyze the following Python snippet.

def another_function(data):
    N = len(data)
    # Loop 1
    for i in range(N):
        print(i)
    # Loop 2
    for j in range(N):
        print(j)

What is the Big OO time complexity of `another_function`?

  • A) O(N2)O(N^2)
  • B) O(2N)O(2N)
  • C) O(N)O(N)
  • D) O(logN)O(\log N)